package com.actitime.multiplebrowser;



import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;

public class CustomersTestTwo {
	public WebDriver driver;
	String expectedValue= "Mayank";
	
	@Test
  public void createCustomer() {
		  //creating customer
		  driver.findElement(By.xpath("//div[contains(text(),'Tasks')]")).click();
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  driver.findElement(By.linkText("Projects & Customers")).click();
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  driver.findElement(By.xpath("//input[@value='Create New Customer']")).click();
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  driver.findElement(By.name("name")).sendKeys(expectedValue);
		  driver.findElement(By.name("createCustomerSubmit")).click();
		  System.out.println(driver.findElement(By.xpath("//span[@class='successmsg']")).getText());
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

	}
	
	
  @BeforeMethod
  public void login()
  {
	 //logging in
	  driver.findElement(By.name("username")).sendKeys("admin");
	  driver.findElement(By.name("pwd")).sendKeys("manager");
	  driver.findElement(By.xpath("//a[@id='loginButton']")).click();
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	 
	  }

  @AfterMethod
  public void deleteCustomer() {
	  	Boolean flag=false;
		WebElement wb = driver.findElement(By.name("selectedCustomer"));
		Select sel = new Select(wb);
		java.util.List<WebElement> lst = sel.getOptions();
		for(int j=0; j<lst.size();j++){
			
			//getting the actual value
			String actualVal=lst.get(j).getText();
			
			//comparing expected and actual value
			if(actualVal.equals(expectedValue)){
				sel.selectByVisibleText(expectedValue);
				flag=true;
				break;
			}
		}
		
		
		if(flag){
			driver.findElement(By.xpath("//td[table[tbody[tr[td[a[contains(text(),'Mayank')]]]]]]/following-sibling::td/input")).click();
			driver.findElement(By.xpath("//input[@value='Delete Selected']")).click();
			driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//input[@id='deleteButton']")).click();
			System.out.println(driver.findElement(By.xpath("//span[@class='successmsg']")).getText());
			System.out.println("=================");
			flag=false;
		} else {
			System.out.println("Customer not present");
		}
	  driver.findElement(By.linkText("Logout")).click();
  }

  @BeforeClass
  public void BeforeClass() {
	  driver = new FirefoxDriver();
	  driver.get("http://127.0.0.1/login.do");
	   }

  @AfterClass
  public void AfterClass() {
	  driver.quit();
  }

}